package com.mashibing.juc.c_025;

import java.util.Arrays;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.CountDownLatch;

public class T01_ConcurrentMap {

    public static void main(String[] args) {
        Map<String ,String> map = new ConcurrentHashMap<>();//ConcurrentHashMap用的是CAS操作，CAS用在TreeMap中特别复杂所以没有ConcurrentTreeMap
//        Map<String ,String> map = new ConcurrentSkipListMap<>();//高并发并且排序，跳表的数据结构
        Random r = new Random();
        Thread[] threads = new Thread[100];

        CountDownLatch latch = new CountDownLatch(threads.length);
        long start = System.currentTimeMillis();
        for (int i = 0; i < threads.length; i++) {
            threads[i] = new Thread(()->{
                for (int j = 0; j < 10000; j++) map.put("a"+r.nextInt(100000),"a"+r.nextInt(100000));
                latch.countDown();
            });
        }
        Arrays.asList(threads).forEach(t->t.start());
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start);
        System.out.println(map.size());
    }
}
